package com.gopay.common.user.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.domain.cust.CustRealNameCertifyLog;
import com.gopay.common.user.dao.CustRealNameCertifyLogDAO;

/**
 * 实名认证日志表
 * @CustRealNameCertifyLogDAOImpl.java
 * @author wanggang
 * @2014-8-1 下午2:51:12  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("custRealNameCertifyLogDAO")
public class CustRealNameCertifyLogDAOImpl extends BaseDAOImpl<CustRealNameCertifyLog, Serializable> implements CustRealNameCertifyLogDAO {

    @Override
    public CustRealNameCertifyLog findByOrderId(String orderId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustRealNameCertifyLog.class);
        criteria.add(Restrictions.eq("orderId",orderId));
        List<CustRealNameCertifyLog> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

    @Override
    public CustRealNameCertifyLog findCustLatestLog(String custId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustRealNameCertifyLog.class);
        criteria.add(Restrictions.eq("custId",custId));
        criteria.addOrder(Order.desc("acceptTime"));
        List<CustRealNameCertifyLog> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

    /**
     * 根据证件号和姓名查询新生实名认证结果
     * @param certNo
     * @param name
     * @return
     */
    @Override
    public CustRealNameCertifyLog findByCertNoAndName(String certNo, String name){
        DetachedCriteria criteria = DetachedCriteria.forClass(CustRealNameCertifyLog.class);
        // + hougn 2017-04-11央行检测敏感信息整改  start
        List<String> certNoList = SensitiveInfoUtils.getAllHisCipherText(certNo);
        List<String> nameList = SensitiveInfoUtils.getAllHisCipherText(name);
        // + hougn 2017-04-11央行检测敏感信息整改  end
        criteria.add(Restrictions.in("certNo",certNoList));
        criteria.add(Restrictions.in("custName",nameList));

        List<CustRealNameCertifyLog> list = this.findByCriteria(criteria);
        return list.size()>0 ?list.get(0):null;
    }

}
